package com.zhugang.week03;

import java.util.Stack;

/**
 * @program algorithms
 * @description: minStack
 * @author: chanzhugang
 * @create: 2022/06/05 08:01
 */
public class MinStack {

    Stack<Integer> data = new Stack<>();
    Stack<Integer> minVal = new Stack<>();

    /**
     * 155.最小栈
     * <p>
     * 定义两个栈：一个栈正常存储；另一个栈操作最小数
     */

    public MinStack() {

    }

    public void push(int val) {
        if (data.isEmpty()) {
            minVal.push(val);
        } else {
            // 维护最小栈
            int curMinVal = minVal.peek();
            minVal.push(val < curMinVal ? val : curMinVal);
        }
        data.push(val);
    }

    public void pop() {
        data.pop();
        minVal.pop();
    }

    public int top() {
        return data.peek();
    }

    public int getMin() {
        return minVal.peek();
    }

}